// const rqcfg = require('constant.js');
const app = getApp();
const request = function (options) {
  var baseUrl = app.globalData.requestUrl;   //基础请求路径
  if (options.useLivePlugin){
    baseUrl = app.globalData.liverqurl;   //用于直播请求路径
  }
  //返回Promise对象
  return new Promise(function (resolve, reject) {
    if (!options.noloading){
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      method: options.method || 'GET',
      url: baseUrl,
      data: options.data || {},
      header: {
        'content-type': 'application/json',
      },
      success: function (res) {
        console.log(baseUrl)
        if (res.data.ec == 200) {
          if (!options.noloading) {
            wx.hideLoading()
          }
          resolve(res);
        } else if (res.data.ec == 400) {
          if (!options.noloading) {
            wx.hideLoading()
          }
          reject(res.data.em);
          if (!options.showTip) {
            wx.showToast({
              title: res.data.em,
              icon: "none"
            })
          }
          //错误信息处理
        } else if (res.data.ec == 450) {  //session失效
          if (!options.noloading) {
            wx.hideLoading()
          }
          app.wechatSq(options.pointer);
          app.globalData.isLogining = true;
        } else {
          if (!options.noloading) {
            wx.hideLoading()
          }
          wx.showToast({
            title: res.data.em,
            icon: "none"
          })
        }
      },
      complete:function(){
        wx.stopPullDownRefresh();
      }
    })
  }
  )
}

module.exports = {
  request: request
}